var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    goodsInfo: {
      goods_name: '',
      price_pay: 0,
      shop_name: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    var orderId = options.orderId;
    this.setData({ orderId: orderId});
    //app.globalData.userInfo.token
    if (options.money && options.goods_name && options.shop_name){
      this.setData({
        goodsInfo: {
          goods_name: options.goods_name,
          price_pay: options.money,
          shop_name: options.shop_name,
        }
      });
    }else{
      util.myRequest(api.GoodsOrderInfo + '/' + orderId, false, function (data) {
        var goodsInfo = data.data;
        that.setData({ goodsInfo: goodsInfo});
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindmessage: function (e) {
    console.log("获取推送消息");
    console.log(e.detail)
  },

  toPay: function(e){
    var that = this;
    var payInfoUrl = api.GoodsPayInfo + '/' + that.data.orderId + '?token=' + app.globalData.userInfo.token
    util.myRequest(payInfoUrl, false, function (data) {
      var payInfo = data.data;
      that.wxPay(payInfo);
    });
  },

  wxPay: function (payInfo) {
    var that = this;
    var showDuration = 2000;
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': 'MD5',
      'paySign': payInfo.paySign,
      'success': function (res) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: showDuration
        })

        setTimeout(function () {

          // wx.redirectTo({
          //   url: './../order/order'
          // })
          wx.switchTab({
            url: './../user_order/order'
          })
          // wx.navigateBack({
          //   delta: 1
          // })
        }, showDuration);
      },
      'fail': function (res) {
        wx.showToast({
          title: '支付失败',
          icon: 'success',
          duration: showDuration
        })

        setTimeout(function () {

          // wx.redirectTo({
          //   url: './../order/order'
          // })
          // wx.navigateBack({
          //   delta: 1
          // })
        }, showDuration);
      }
    })
  }


})